package com.jieao.template.controller;

import cn.hutool.core.util.IdUtil;
import com.jieao.template.common.util.CurrentlyLoggedInUtil;
import com.jieao.template.common.util.JsonUtils;
import com.jieao.template.entity.pub.JsonData;
import com.jieao.template.entity.pub.Pagination;
import com.jieao.template.entity.roleMgr.AssignmentPermissionDTO;
import com.jieao.template.entity.roleMgr.RoleDO;
import com.jieao.template.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * 角色表(Role)表控制层
 *
 * @author jzw
 * @since 2020-07-15 14:50:59
 */
@Slf4j
@Validated
@Api(value = "角色表模块接口", tags = "角色表模块接口")
@RestController
@RequestMapping("/api/v1/role")
public class RoleController
{
    /**
     * 服务对象
     */
    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "给角色分配权限", notes = "给角色分配权限")
    @PostMapping("assignmentPermissionForRole")
    JsonData assignmentPermissionForRole(@RequestBody AssignmentPermissionDTO assignmentPermissionDTO)
    {
        log.info("给角色分配权限, 参数:{}, 操作人账户:{}", assignmentPermissionDTO, CurrentlyLoggedInUtil.getAccount());
        roleService.assignmentPermissionForRole(assignmentPermissionDTO);
        return JsonData.ok();
    }

    @PatchMapping("disOrEnaAccount")
    @ApiOperation(value = "启禁用角色", notes = "启禁用角色")
    JsonData disOrEnaAccount(@ApiParam(name = "roleId", value = "账户角色", required = true) @NotBlank(message = "账号id不能为空") String roleId)
    {
        log.info("启禁用角色, 参数:{}, 操作人账户:{}", roleId, CurrentlyLoggedInUtil.getAccount());
        roleService.disOrEnaAccount(roleId);
        return JsonData.ok();
    }

    @ApiOperation(value = "获取当前角色已选的权限", notes = "获取当前角色已选的权限")
    @GetMapping("listCheckedPermissionForRole")
    JsonData listCheckedPermissionForRole(@ApiParam(name = "id", value = "角色id", required = true) String id)
    {
        log.info("获取当前角色已选的权限, 参数:{}, 操作人账户:{}", id, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok(roleService.listCheckedPermissionForRole(id));
    }

    @GetMapping("roleByUserId")
    @ApiOperation(value = "获取用户关联的角色", notes = "获取用户关联的角色")
    JsonData getRoleByUserId(String id)
    {
        log.info("获取用户关联的角色, 参数:{}, 操作人账户:{}", id, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok(roleService.getRoleByUserId(id));
    }

    /**
     * @author jzw
     * @since 2020-07-15 14:50:59
     */
    @GetMapping("/selectOne")
    @ApiOperation(value = "查询单条数据", notes = "通过主键查询Role表单条数据")
    public JsonData selectOne(@ApiParam(name = "id", value = "Role表id", required = true) @NotBlank(message = "id不能为空") String id)
    {
        log.info("通过id查询Role表单条数据, id:{}, 操作人账户:{}", id, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok(roleService.queryById(id));
    }

    /**
     * @author jzw
     * @since 2020-07-15 14:50:59
     */
    @GetMapping("/queryAll")
    @ApiOperation(value = "查询全部数据", notes = "可通过roleDO对象包含的参数进行数据筛选")
    public JsonData queryAll(@RequestParam Map<String, Object> params)
    {
        RoleDO role = JsonUtils.map2pojo(params, RoleDO.class);
        log.info("查询Role表数据, 参数:{}, 操作人账户:{}", role, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok(roleService.queryAll(role));
    }

    /**
     * @author jzw
     * @since 2020-07-15 14:50:59
     */
    @ApiOperation(value = "查询分页数据", notes = "可通过Pagination对象传递分页信息及其他参数进行数据筛选")
    @GetMapping("/queryPageList")
    public JsonData queryPageList(Pagination pagination)
    {
        log.info("分页查询Role表数据, 参数:{}, 操作人账户:{}", pagination, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok(roleService.queryAllByLimit(pagination));
    }

    /**
     * @author jzw
     * @since 2020-07-15 14:50:59
     */
    @ApiOperation(value = "新增数据", notes = "新增一条Role表数据")
    @PostMapping("/insert")
    public JsonData insert(@RequestBody RoleDO role)
    {
        role.setId(IdUtil.simpleUUID());
        roleService.insert(role);
        log.info("新增Role表数据, 参数:{}, 操作人账户:{}", role, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok();
    }

    /**
     * @author jzw
     * @since 2020-07-15 14:50:59
     */
    @ApiOperation(value = "修改数据", notes = "修改一条Role表数据")
    @PutMapping("/update")
    public JsonData update(@RequestBody RoleDO role)
    {
        roleService.update(role);
        log.info("修改Role表数据, 参数:{}, 操作人账户:{}", role, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok();
    }

    /**
     * @author jzw
     * @since 2020-07-15 14:50:59
     */
    @ApiOperation(value = "删除数据", notes = "通过主键假删除一条Role表数据")
    @DeleteMapping("/deleteById")
    public JsonData deleteById(@ApiParam(name = "id", value = "Role表id", required = true) String id)
    {
        roleService.deleteById(id);
        log.info("删除单条Role表数据, 参数:{}, 操作人账户:{}", id, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok();
    }

    /**
     * @author jzw
     * @since 2020-07-15 14:50:59
     */
    @ApiOperation(value = "批量删除数据", notes = "通过主键集合批量假删除Role表数据")
    @DeleteMapping("/batchDeletionByIdList")
    public JsonData batchDeletionByIdList(@RequestParam List<String> idList)
    {
        roleService.batchDeletionByIdList(idList);
        log.info("批量删除Role表数据, 参数:{}, 操作人账户:{}", idList, CurrentlyLoggedInUtil.getAccount());
        return JsonData.ok();
    }

}